package commons.test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class DummyServerSocket extends ServerSocket {

	private List<Socket>	socketsAccept;
	private long			timeToWaitAfterLastExpectedConnection;

	public DummyServerSocket(final int port) throws IOException {
		super(port);
	}

	@Override
	public Socket accept() {

		if (socketsAccept.isEmpty()) {
			try {
				Thread.sleep(timeToWaitAfterLastExpectedConnection);
			} catch (final InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		return socketsAccept.remove(0);
	}

	public void setSocketsAccept(final List<Socket> socketsAccept) {
		this.socketsAccept = new ArrayList<Socket>(socketsAccept);
	}

	public void setTimeToWaitAfterLastExpectedConnection(final long time) {
		timeToWaitAfterLastExpectedConnection = time;
	}
}